package com.zqx.sharestate.context;

import com.zqx.sharestate.abstractstate.SwitchState;
import com.zqx.sharestate.concretestate.OffState;
import com.zqx.sharestate.concretestate.OnState;
import lombok.extern.slf4j.Slf4j;

/**
 * description: 环境类
 * @author zqx
 * @date 2018/09/06
 */
@Slf4j
public class Switch {

    private static SwitchState currentState,onState,offState;//定义3个静态的状态对象
    private String name;

    public Switch(String name) {
        this.name = name;
        onState = new OnState();
        offState = new OffState();
        currentState = onState;
    }

    public void setState(SwitchState state){
        currentState = state;
    }
    public static SwitchState getState(String type){
        if(type.equalsIgnoreCase("on")){
            return onState;
        }else {
            return offState;
        }
    }

    //打开开关
    public void on(){
        /*log.info("【环境类】打开开关方法,{}",name);*/
        System.out.println("【环境类】打开开关方法"+name);
        currentState.on(this);
    }

    //关闭开关
    public void off(){
        /*log.info("【环境类】关闭开关方法,{}",name);*/
        System.out.println("【环境类】关闭开关方法"+name);
        currentState.off(this);
    }
}
